class myPromise {
    constructor(fn) {
        const _this = this
        this.__queue = []
        this.__succ_res = null
        this.__error_res = null
        this.status = ''

        fn(function (...arg) {
            _this.__succ_res = arg
            _this.status = 'succ'

            _this.__queue.forEach(json => {
                json.fn1(...arg)
            })
        },
        function (...arg) {
            _this.__error_res = arg
            _this.status = 'error'

            _this.__queue.forEach(json => {
                json.fn2(...arg)
            })
        })
    }

    then(fn1, fn2) {
        if (this.status === 'succ') {
            fn1(...this.__succ_res)
        } else if (this.status === 'error') {
            fn2(...this.__error_res)
        } else {
            this.__queue.push({fn1, fn2})
        }
    }
}

myPromise.all = function (arr) {
    let aResult = []
    return myPromise(function (resolve, reject) {
        let i = 0
        next()
        function next () {
            arr[i].then(function (res) {
                aResult.push(res)
                i++
                if (i === arr.length) {
                    resolve(aResult)
                } else {
                    next()
                }
            }, reject)
        }
    })
}

let mp = new myPromise((resolve, reject) => {
    setTimeout(function () {resolve(12)}, 500)
})

mp.then(res => { console.log(res) }, err => { console.log(err) })